import { defineStore } from 'pinia'
import * as api from '@/api/post'

export const postStore = defineStore('postStore', {
  state: () => ({
    //岗位列表
    postList: [],
    //岗位详情
    // postInfo: {},
    pagination: {
        page: 1,
        limit: 6,
        total: 0,
      },
  }),
  actions: {
    // 获取岗位列表-带分页
    async getPostPage(param) {
      try {
        const params = {
            ...param,
            page: this.pagination.page,
            limit: this.pagination.limit
        }
        const res = await api.getPostPage(params)
        this.postList = res.list || []
        this.pagination.total = res.total
        return res
      } catch(error) {
        throw error
      }
    },

    // 获取岗位详情
    async getPostInfo(id) {
      try {
        const res = await api.getPostOne(id)
        // this.postInfo = res || {}
        return res
      } catch(error) {
        throw error
      }
    },

    // 新增岗位
    async addPost(dataForm) {
      try {
        const res = await api.addPost(dataForm)
        return res
      } catch(error) {
        throw error
      }
    },

    // 编辑岗位
    async editPost(dataForm) {
      try {
        const res = await api.editPost(dataForm)
        return res
      } catch(error) {
        throw error
      }
    },

    async savePost(dataForm) {
      {
        if (dataForm.id) {
          return this.editPost(dataForm)
        } else {
          return this.addPost(dataForm)
        }
      }
    },

    // 删除岗位
    async deleteBatch(ids) {
      try {
        const res = await api.deletePost(ids)
        return res
      } catch(error) {
        throw error
      }
    },

    // 这个接口好像没用，因为 this.postList 已经获取到了所有岗位列表
    async getPostList() {
      try {
        const res = await api.getPostList()
        return res || []
      } catch(error) {
        throw error
      }
    }
  }

})